﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Web;

namespace raya.Library
{
    public class AzureBlob
    {
#if AZURE
        public static readonly AzureBlob Init = new AzureBlob();

        private AzureBlob()
        {
            CreateBlobContainer(Img.blobImageContainerName);
        }

        public static string GetBlobItemUri(string containerName, string itemName)
        {
            var container = GetBlobContainer(Img.blobImageContainerName);
            string thumbUri;

            //account for local dev store style URIs
            if (container.UsePathStyleUris)
            {
                thumbUri = new Uri(container.BaseUri, Path.Combine(container.AccountName, Path.Combine(containerName, itemName))).ToString();
            }
            else
            {
                thumbUri = new Uri(new Uri("http://" + container.AccountName + "." + container.BaseUri.Authority), Path.Combine(containerName, itemName)).ToString();
            }
            return thumbUri;
        }

        public static bool AddBlobItem(BlobContainer container, BlobProperties properties, BlobContents contents, bool overwrite)
        {
            return container.CreateBlob(properties, contents, overwrite);
        }

        public static BlobContainer CreateBlobContainer(string name)
        {
            var container = GetBlobContainer(name);
            if (!container.DoesContainerExist())
            {
                var ok = container.CreateContainer(null, ContainerAccessControl.Public);
                if (!ok)
                {
                    throw new Exception("failed to create blob storage");
                }
            }
            return container;
        }

        public static bool DeleteBlobItem(string containerName, string itemName)
        {
            if (DoesBlobItemExists(containerName,itemName))
            {
                var container = GetBlobContainer(containerName);
                return container.DeleteBlob(itemName);
            }
            return true;
        }

        public static bool DoesBlobItemExists(string containerName, string itemName)
        {
            var container = GetBlobContainer(containerName);
            return container.ListBlobs(itemName, false).Count() != 0;
        }

        public static BlobContainer GetBlobContainer(string containerName)
        {
            return BlobStorage.Create(StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration())
                .GetBlobContainer(containerName);
        }

        public static BlobContents GetBlobContent(string containerName, string itemName)
        {
            var container = GetBlobContainer(containerName);
            var ms = new MemoryStream();
            var contents = new BlobContents(ms);
            var props = container.GetBlob(itemName, contents, false);
            return contents;
        }

        public static bool CopyContent(string containerName, string sourceName, string destName)
        {
            var container = GetBlobContainer(containerName);
            var ms = new MemoryStream();
            var contents = new BlobContents(ms);
            var props = container.GetBlob(sourceName, contents, false);
            var destProps = new BlobProperties(destName)
                                {
                                    ContentType = props.ContentType,
                                };
            var destContents = new BlobContents(contents.AsStream);
            return container.CreateBlob(destProps, destContents, false);
        }
#endif
    }
}
